﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Task
{
    class Resizing
    {
        private struct point_X_Y
        {
            public int x;
            public int y;
        };

        public myclr[,] bilinear_resize(int old_width, int old_height, int new_width, int new_height, myclr[,]  old_pic)
        {
            myclr[,] new_pic=new myclr[new_width,new_height];

            double Wfact = (old_width*1.0) / (new_width*1.0);
            double Hfact = (old_height * 1.0) / (new_height * 1.0);
            double old_x, old_y, xFact, yFact, z1, z2;
            int x1, x2, y1, y2;
            point_X_Y p1, p2, p3, p4;

            for (int i = 0; i < new_width; i++)
            {
                for (int j = 0; j < new_height; j++)
                {

                    old_x = i * Wfact;
                    old_y = j * Hfact;
                    
                    x1 = (int)(old_x);//upper left corner
                    y1 = (int)(old_y);//upper left corner

                    if (x1 < old_x - 2)
                        x2 = x1 + 1;
                    else
                        x2 = x1;
                    
                    if (y1 < old_y - 2)
                        y2 = y1 + 1;
                    else
                        y2 = y1;

                    
                   
                    //upper left corner
                    p1.x = x1;
                    p1.y = y1;

                    //upper right corner
                    p2.x = x2;
                    p2.y = y1;

                    //lower left corner
                    p3.x = x1;
                    p3.y = y2;


                    //lower right corner
                    p4.x = x2;
                    p4.y = y2;


                    xFact = old_x - x1;
                    yFact = old_y - y1;


                    //getting avg for Red colour
                    z1 = ((old_pic[p1.x, p1.y].r) * (1 - xFact)) +( (old_pic[p2.x, p2.y].r) * (xFact));
                    z2 = ((old_pic[p3.x, p3.y].r) * (1 - xFact)) +( (old_pic[p4.x, p4.y].r) * (xFact));

                    new_pic[i, j].r = (int)((z1 * (1 - yFact)) + (z2 * (yFact)));
                    //getting avg for green colour
                    z1 = ((old_pic[p1.x, p1.y].g) * (1 - xFact)) + ((old_pic[p2.x, p2.y].g) * (xFact));
                    z2 = ((old_pic[p3.x, p3.y].g) * (1 - xFact)) + ((old_pic[p4.x, p4.y].g) * (xFact));

                    new_pic[i, j].g = (int)((z1 * (1 - yFact)) + (z2 * (yFact)));
                    //getting avg for blue colour
                    z1 = ((old_pic[p1.x, p1.y].b) * (1 - xFact)) + ((old_pic[p2.x, p2.y].b) * (xFact));
                    z2 = ((old_pic[p3.x, p3.y].b) * (1 - xFact)) + ((old_pic[p4.x, p4.y].b) * (xFact));

                    new_pic[i, j].b = (int)((z1 * (1 - yFact)) + (z2 * (yFact)));

                }
            }

            return new_pic;
        }

    }

}
